//
//  DeviceViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/19.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation
import TXLiteAVSDK_TRTC_Mac


class DeviceViewController: BaseTRTCViewController {
    
    
    var speakerList:[AnyObject] = []
    var micList:[AnyObject] = []
    var cameraList:[AnyObject] = []
    var basicList: [String] = []
    
    var curspeakerId:String?
    var curmicId:String?
    var curcameraId:String?
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 240, 200))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        self.curspeakerId = TRTCCloud.sharedInstance().getCurrentSpeakerDevice()?.deviceId ?? ""
        self.curmicId = TRTCCloud.sharedInstance()?.getCurrentMicDevice()?.deviceId ?? ""
        self.curcameraId = TRTCCloud.sharedInstance()?.getCurrentCameraDevice()?.deviceId ?? ""
        
        self.tabView.tabView.kreloadData()
    }
    
    func setData(speakerList:[AnyObject]=[],
                 micList:[AnyObject]=[],
                 cameraList:[AnyObject]=[],
                 basicList: [String]) {
        self.speakerList = speakerList
        self.micList = micList
        self.cameraList = cameraList
        self.basicList = basicList
        
        var count = speakerList.count+micList.count+cameraList.count+basicList.count
        if speakerList.count > 0 {
            self.speakerList.insert("选择扬声器" as AnyObject, at: 0)
            count+=1
        }
        if micList.count > 0 {
            self.micList.insert("选择麦克风"as AnyObject, at: 0)
            count+=1
        }
        if cameraList.count > 0 {
            self.cameraList.insert("选择摄像机"as AnyObject, at: 0)
            count+=1
        }
        self.view.setFrameSize(NSSize.init(width: 240, height: CGFloat(count)*24+18+10))
    }
  
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_gray(240).cgColor
        
        self.view.addSubview(tabView)
        tabView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    lazy var tabView: ZHTableView = {
        let tab = ZHTableView.init(frame: NSRect.zero)
        tab.delegate = self
        tab.dataSourse = self
        return tab
    }()
    
    func dataListCount() -> Int {
        return self.speakerList.count+self.micList.count+self.cameraList.count+self.basicList.count
    }
    
}

extension DeviceViewController :ZHTableViewDelegate,ZHTableViewDataSource {
    func zh_tableView(_ tableView: ZHTableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataListCount()
    }
    
    func zh_tableView(_ tableView: ZHTableView, cellForRowAt indexPath: IndexPath) -> NSView? {
        
        var cell = tableView.tabView.makeView(withIdentifier: ZHTableViewCell.identifier, owner: self) as? ZHTableViewCell
        if cell == nil {
            cell = ZHTableViewCell.init(identifier: ZHTableViewCell.identifier)
        }
        self.setListTypeAndData(row: indexPath.item, cell: cell)
        return cell
    }
    
    func setListData(row:Int,cell:ZHTableViewCell?,deviType:Int)  {
        switch deviType {
        case 1000:
            let info = self.speakerList[safe:row]
            if let device = info as? TRTCMediaDeviceInfo {
                cell?.stitle.stringValue = device.deviceName ?? "未知设备"
                cell?.multipleSelect = device.deviceId == self.curspeakerId
            }else {
                cell?.stitle.stringValue = (info as? String) ?? "..."
            }
        case 2000:
            let info = self.micList[safe:row]
            if let device = info as? TRTCMediaDeviceInfo {
                cell?.stitle.stringValue = device.deviceName ?? "未知设备"
                cell?.multipleSelect = device.deviceId == self.curmicId
            }else {
                cell?.stitle.stringValue = (info as? String) ?? "..."
            }
        case 3000:
            let info = self.cameraList[safe:row]
            if let device = info as? TRTCMediaDeviceInfo {
                cell?.stitle.stringValue = device.deviceName ?? "未知设备"
                cell?.multipleSelect = device.deviceId == self.curcameraId
            }else {
                cell?.stitle.stringValue = (info as? String) ?? "..."
            }
        default:
            break
        }
    }
    
    func setListType(index:Int,count:Int,cell:ZHTableViewCell?) {
        if index == 0 {
            cell?.type = .group
        }else {
            cell?.type = .multiple
            cell?.setHover()
            if index == count-1 {
                cell?.setBottomLine()
            }
        }
    }
    
    func setListTypeAndData(row:Int,cell:ZHTableViewCell?) {
        
        let col1 = self.speakerList.count
        let col2 = col1 + self.micList.count
        let col3 = col2 + self.cameraList.count
        let col4 = col3 + self.basicList.count
        
        switch row {
        case 0..<col1:
            let index = row-0
            if self.speakerList.count >= 0 {
                self.setListType(index: index, count: self.speakerList.count, cell: cell)
                self.setListData(row: index,cell: cell, deviType: 1000)
            }
        case col1..<col2:
            let index = row-col1
            if self.micList.count >= 0 {
                self.setListType(index: index, count: self.micList.count, cell: cell)
                self.setListData(row: index, cell: cell, deviType: 2000)
            }
        case col2..<col3:
            let index = row-col2
            if self.cameraList.count >= 0 {
                self.setListType(index: index, count: self.cameraList.count, cell: cell)
                self.setListData(row: index, cell: cell, deviType: 3000)
            }
        case col3..<col4:
            let index = row-col3
            cell?.type = .default
            cell?.stitle.stringValue = self.basicList[index]
            cell?.setHover()
            if index == self.basicList.count-2 {
                cell?.setBottomLine()
            }
        default:
            break
        }
    }
    
    func zh_tableView(_ tableView: ZHTableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 24
    }
    func zh_tableView(_ tableView: ZHTableView, shouldSelectRow row: Int) -> Bool {
        let rowView = tableView.tabView.rowView(atRow: row, makeIfNecessary: false)
        rowView?.selectionHighlightStyle = .regular
        rowView?.isEmphasized = false
        return true
    }
    
    func zh_tableView(_ tableView: ZHTableView, didSelectRowAt indexPath: IndexPath) {
        let row = indexPath.item
        let col1 = self.speakerList.count
        let col2 = col1 + self.micList.count
        let col3 = col2 + self.cameraList.count
        let col4 = col3 + self.basicList.count
        switch row {
        case 0..<col1:
            let index = row
            if let deviInfo = self.speakerList[safe:index] as? TRTCMediaDeviceInfo {
                TRTCCloud.sharedInstance()?.setCurrentSpeakerDevice(deviInfo.deviceId)
                self.curspeakerId = deviInfo.deviceId
                
            }
        case col1..<col2:
            let index = row-col1
            if let deviInfo = self.micList[safe:index] as? TRTCMediaDeviceInfo {
                TRTCCloud.sharedInstance()?.setCurrentMicDevice(deviInfo.deviceId)
                self.curmicId = deviInfo.deviceId
            }
        case col2..<col3:
            let index = row-col2
            if let deviInfo = self.cameraList[safe:index] as? TRTCMediaDeviceInfo {
                TRTCCloud.sharedInstance()?.setCurrentCameraDevice(deviInfo.deviceId)
                self.curcameraId = deviInfo.deviceId
            }
        case col3..<col4:
            let index = row-col2
            let string = self.basicList[index]
            print("string\(string)")
            if self.cameraList.count>0 {
                
            }else {
                
            }
        default:
            break
        }
        
        Lk.shared.dismiss()
    }
    
    func ount(count:Int) -> Int {
        return count > 0 ? count+1:count
    }
    
}
